package com.sky.announce.service.impl;

import java.util.List;
import com.sky.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.sky.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.sky.announce.domain.Person;
import com.sky.announce.mapper.AnnounceMapper;
import com.sky.announce.domain.Announce;
import com.sky.announce.service.IAnnounceService;

/**
 * 公告管理Service业务层处理
 * 
 * @author Kunpeng
 * @date 2025-07-14
 */
@Service
public class AnnounceServiceImpl implements IAnnounceService 
{
    @Autowired
    private AnnounceMapper announceMapper;

    /**
     * 查询公告管理
     * 
     * @param id 公告管理主键
     * @return 公告管理
     */
    @Override
    public Announce selectAnnounceById(Long id)
    {
        return announceMapper.selectAnnounceById(id);
    }

    /**
     * 查询公告管理列表
     * 
     * @param announce 公告管理
     * @return 公告管理
     */
    @Override
    public List<Announce> selectAnnounceList(Announce announce)
    {
        return announceMapper.selectAnnounceList(announce);
    }

    /**
     * 新增公告管理
     * 
     * @param announce 公告管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAnnounce(Announce announce)
    {
        announce.setCreateTime(DateUtils.getNowDate());
        int rows = announceMapper.insertAnnounce(announce);
        insertPerson(announce);
        return rows;
    }

    /**
     * 修改公告管理
     * 
     * @param announce 公告管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAnnounce(Announce announce)
    {
        announce.setUpdateTime(DateUtils.getNowDate());
        announceMapper.deletePersonByAnnounceId(announce.getId());
        insertPerson(announce);
        return announceMapper.updateAnnounce(announce);
    }

    /**
     * 批量删除公告管理
     * 
     * @param ids 需要删除的公告管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAnnounceByIds(Long[] ids)
    {
        announceMapper.deletePersonByAnnounceIds(ids);
        return announceMapper.deleteAnnounceByIds(ids);
    }

    /**
     * 删除公告管理信息
     * 
     * @param id 公告管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAnnounceById(Long id)
    {
        announceMapper.deletePersonByAnnounceId(id);
        return announceMapper.deleteAnnounceById(id);
    }

    /**
     * 新增人员管理信息
     * 
     * @param announce 公告管理对象
     */
    public void insertPerson(Announce announce)
    {
        List<Person> personList = announce.getPersonList();
        Long id = announce.getId();
        if (StringUtils.isNotNull(personList))
        {
            List<Person> list = new ArrayList<Person>();
            for (Person person : personList)
            {
                person.setAnnounceId(id);
                list.add(person);
            }
            if (list.size() > 0)
            {
                announceMapper.batchPerson(list);
            }
        }
    }
}
